/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 5/27/13
 * Time: 11:31 PM
 * To change this template use File | Settings | File Templates.
 */
var Bookmark = Class.create({
    initialize: function(){

        this.bookmarks = new Array();
    },
    load: function(data){
        var _this = this;

        if(data != undefined && data!=""){
            this.bookmarks = data;
        }

        //Khoi tao view bookmark
        $j.each(this.bookmarks, function(index, value){
            var liTag = new Element("li", {bookId: value.bookId, pageId: value.pageId});
            var img = $j("<img>").attr('src', value.coverImage).attr({width: "16", height: "16"}).addClass("ui-li-icon");
            var aTag = $j("<a href='#'></a>").append(img).append(value.bookName + "--" + value.pageId);
            var splitaTag = $j("<a>Remove</a>");

            $j(splitaTag).on("tap", function(){
                _this.remove(value);
            });

            $j(aTag).on("tap", function(){
                Global.collections.viewBook(value.bookId, value.pageId);
            });

            $j(liTag).append(aTag).append(splitaTag).appendTo("#" + Static.bookmarkList);
        });

        $j("#"+Static.bookmarkList).listview("refresh");
    },
    add: function(value){
        var _this = this;

        //data--> { bookId, pageId, bookName, coverImage}
        for(var i = 0 ; i < this.bookmarks.length; i++){
            var j = this.bookmarks[i];
            if(j.bookId == value.bookId && j.pageId == value.pageId){
                return;
            }
        }

        this.bookmarks.push(value);

        var liTag = new Element("li", {bookId: value.bookId, pageId: value.pageId});
        var img = $j("<img>").attr('src', value.coverImage).attr({width: "16", height: "16"}).addClass("ui-li-icon");
        var aTag = $j("<a href='#'></a>").append(img).append(value.bookName + "--" + value.pageId);
        var splitaTag = $j("<a>Remove</a>");

        $j(splitaTag).on("tap", function(){
            _this.remove(value);
        });

        $j(aTag).on("tap", function(){
            Global.collections.viewBook(value.bookId, value.pageId);
        });

        $j(liTag).append(aTag).append(splitaTag).appendTo("#" + Static.bookmarkList);

        $j("#"+Static.bookmarkList).listview("refresh");

        this.save();
    },
    remove: function(info){
        for(var i = 0 ; i < this.bookmarks.length; i++){
            var j = this.bookmarks[i];
            if(j.bookId == info.bookId && j.pageId == info.pageId){
                var liTag = $j("#"+Static.bookmarkList + " li[bookId=" + info.bookId +"][pageId=" + info.pageId+"]");
                $j(liTag).remove();

                this.bookmarks[i] = null;
                this.bookmarks = this.bookmarks.compact();
                $j("#"+Static.bookmarkList).listview("refresh");
                break;
            }
        }

        this.save();
    },
    save: function(){
        MyTools.saveTextPersistent("bookmark.json", $j.toJSON(this.bookmarks));
    }
});